package com.znsd.zl.collect.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.browse.entity.vo.TBrowseEntityVo;
import com.znsd.zl.collect.dao.CollectDao;
import com.znsd.zl.collect.entity.CollectEntity;
import com.znsd.zl.collect.service.CollectService;
import com.znsd.zl.house.dao.HouseImageDao;
import com.znsd.zl.house.entity.HouseImageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CollectServiceImpl
 * @Description 收藏service实现类
 * @author xyx
 * @date 2020-08-28 14:48:33
 * @Version v1.0
 */
@Service("tCollectService")
public class CollectServiceImpl extends ServiceImpl<CollectDao, CollectEntity> implements CollectService {

	@Autowired
	HouseImageDao imageDao;

	@Override
	public boolean collectSave(CollectEntity entity) {
		System.out.println(entity.toString());
		List<CollectEntity> byAll = baseMapper.findByAll();
		for (int i = 0; i < byAll.size(); i++) {
			if(entity.getHouseId().equals(byAll.get(i).getHouseId())&&entity.getUserId().equals(byAll.get(i).getUserId())){
				System.out.println("取消添加");
				return false;
			}
		}
		/*格式化时间*/
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*设置创建时间*/
		String format = df.format(new Date());
		Date parse = null;
		try {
			parse = df.parse(format);
			entity.setCreateTime(parse);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return this.save(entity);
	}

	@Override
	public boolean deleteCollect(CollectEntity entity) {
		return baseMapper.deleteCollect(entity);
	}

	@Override
	public List<TBrowseEntityVo> findList(Integer appid) {
		List<TBrowseEntityVo> collect = baseMapper.findCollect(appid);

		return collect;
	}

	@Override
	public boolean findStatus(CollectEntity entity) {
		List<CollectEntity> byAll = baseMapper.findByAll();
		for (int i = 0; i < byAll.size(); i++) {
			if(entity.getHouseId().equals(byAll.get(i).getHouseId())&&entity.getUserId().equals(byAll.get(i).getUserId())){
				return true;
			}
		}
		return false;
	}

	@Override
	public void findImages(Integer houseId) {
		List<HouseImageEntity> byHouseImage = imageDao.findByHouseImage(houseId);
		for (int i = 0; i < byHouseImage.size(); i++) {
			System.out.println(byHouseImage.get(i).toString());
		}
	}
}
